//
//  MovieViewController.m
//  悦享
//
//  Created by dllo on 16/3/18.
//  Copyright © 2016年 张洪国. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieSecZoneCollectionReusableView.h"
#import "TVModel.h"
#import "TVHomeModel.h"
#import "DCPicScrollView.h"
#import "DCWebImageManager.h"
#import "SecViewController.h"
#import "HGGunCollectionViewCell.h"
#import "HGTitleCollectionViewCell.h"
#import "HGNormalCollectionViewCell.h"
#import "HGLargeNormalCollectionViewCell.h"
#import "HGNormalAvatorCollectionViewCell.h"
#import "HGMoviePorCollectionViewCell.h"
#import "HGTextCollectionViewCell.h"
@interface MovieViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, HGGunCollectionViewCellDelegate, HGNormalCollectionViewCellDelegate, HGNormalAvatorCollectionViewCellDelegate, HGMoviePorCollectionViewCellDelegate>

@property (nonatomic, retain) UICollectionView *collection;/**<  底层collection */
@property (nonatomic, retain) NSMutableArray *bannerArr;//轮播图数据

@property (nonatomic, retain) NSMutableArray *textRollLinkArr;//小轮播数据

@property (nonatomic, retain) NSMutableArray *dataArr;//总数据数组

@property (nonatomic, assign) CGFloat contentOffentY;/**<  记录y的偏移量 */

@property (nonatomic, retain) UIView *topView;/**<  查看全部电影的view */

@property (nonatomic, retain) UIButton *topButton;/**<  查看全部电影的button */

@end

@implementation MovieViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.contentOffentY = 10.0;
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor redColor];
    
    self.dataArr = [NSMutableArray array];
    
    [self handleData];
    [self createCollection];
    
    //整理视图的方法
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self createTopView];
    
}

///实现协议跳转方法
- (void)jumpToWebVcwithUrl:(NSString *)url{
    
    SecViewController *sec = [[SecViewController alloc] init];
    sec.url = url;
    [self presentViewController:sec animated:YES completion:^{
        
    }];
    
}
///协议跳转
- (void)jumpToVcWithUrl:(NSString *)url{
    SecViewController *sec = [[SecViewController alloc] init];
    sec.url = url;
    [self presentViewController:sec animated:YES completion:^{
        
    }];
}
- (void)jumpAVtoWebVcWithUrl:(NSString *)url{
    SecViewController *sec = [[SecViewController alloc] init];
    sec.url = url;
    [self presentViewController:sec animated:YES completion:^{
        
    }];
}

- (void)jumpMovieWithUrl:(NSString *)url{
    SecViewController *sec = [[SecViewController alloc] init];
    sec.url = url;
    [self presentViewController:sec animated:YES completion:^{
        
    }];
}

///创建轮播图
- (void)createLunBo{
    NSMutableArray *arrPic = [NSMutableArray array];
    NSMutableArray *arrTitle = [NSMutableArray array];
    for (TVModel *model in _bannerArr) {
        [arrPic addObject:model.picUrl];
        [arrTitle addObject:model.name];
    }
    
    DCPicScrollView *picView = [DCPicScrollView picScrollViewWithFrame:CGRectMake(0, 0, SREEN_WIDTH, 200) WithImageUrls:arrPic];
    picView.titleData = arrTitle;
    picView.placeImage = [UIImage imageNamed:PLACE_HLODER];
    
    picView.AutoScrollDelay = 3;
    
    [_collection addSubview:picView];
    
    [[DCWebImageManager shareManager] setDownloadImageRepeatCount:1];
    
    picView.imageViewDidTapAtIndex = ^(NSInteger index){
        
        TVModel *modal = _bannerArr[index];
        
        SecViewController *sec = [[SecViewController alloc] init];
        sec.url = modal.playUrl;
        
        sec.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        
        [self presentViewController:sec animated:YES completion:^{
            
        }];
        
    };
    
    
    
}


///创建collectionview
- (void)createCollection{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SREEN_WIDTH, SREEN_HEIGHT -64) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1];
    [self.view addSubview:_collection];
    
    [_collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    
    [_collection registerClass:[HGGunCollectionViewCell class] forCellWithReuseIdentifier:@"HGGunCollectionViewCell"];
    
    [_collection registerClass:[HGTitleCollectionViewCell class] forCellWithReuseIdentifier:@"HGTitleCollectionViewCell"];
    
    [_collection registerClass:[HGLargeNormalCollectionViewCell class] forCellWithReuseIdentifier:@"HGLargeNormalCollectionViewCell"];
    
    [_collection registerClass:[HGNormalCollectionViewCell class] forCellWithReuseIdentifier:@"HGNormalCollectionViewCell"];
    
    [_collection registerClass:[HGNormalAvatorCollectionViewCell class] forCellWithReuseIdentifier:@"HGNormalAvatorCollectionViewCell"];
    
    [_collection registerClass:[HGMoviePorCollectionViewCell class] forCellWithReuseIdentifier:@"HGMoviePorCollectionViewCell"];
    
    
    [_collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionReusableView"];
    
    [_collection registerClass:[MovieSecZoneCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MovieSecZoneCollectionReusableView"];
    
    
    [_collection registerClass:[HGTextCollectionViewCell class] forCellWithReuseIdentifier:@"HGTextCollectionViewCell"];
    
}

///collectionView的代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataArr.count-1;
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (_collection.contentOffset.y > _contentOffentY) {
        
        [UIView animateWithDuration:0.5 animations:^{
            _topView.frame = CGRectMake(0, 64, SREEN_WIDTH, 30);
        }];
        
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            _topView.frame = CGRectMake(0, 0, SREEN_WIDTH, 30);
        }];
    }
    
    _contentOffentY = _collection.contentOffset.y;
}
///创建查看全部电影的漂浮view

- (void)createTopView{
    
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SREEN_WIDTH, 30)];
    _topView.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1];
    
    [self.view addSubview:_topView];
    
    self.topButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _topButton.frame = CGRectMake((SREEN_WIDTH - 200)/2, 2, 200, 26);
    [_topButton setTitleColor:[UIColor colorWithRed:0.88 green:0.4 blue:0 alpha:1] forState:UIControlStateNormal];
    [_topButton setTitle:_lookStr forState:UIControlStateNormal];
    
    [_topButton addTarget:self action:@selector(TopbuttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [_topView addSubview:_topButton];
    
}
///点击方法
- (void)TopbuttonAction:(UIButton *)button{
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.row == 0) {
        HGGunCollectionViewCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:@"HGGunCollectionViewCell" forIndexPath:indexPath];
        cell.delegate = self;
        cell.arr = _textRollLinkArr;
        return cell;
  
    } else {
        
        TVHomeModel *model = _dataArr[indexPath.row - 1];
        
        if ([model.type isEqualToString:@"title"]) {
            
            HGTitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HGTitleCollectionViewCell" forIndexPath:indexPath];
            
            cell.backgroundColor = [UIColor whiteColor];
            cell.arr = model.arrModel;
            
            return cell;
        } else if ([model.type isEqualToString:@"normalLandScape"] || [model.type isEqualToString:@"normalLandScapeNodesc"]){
            
            HGNormalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HGNormalCollectionViewCell" forIndexPath:indexPath];
            cell.delegate = self;
            cell.arr = model.arrModel;
            cell.backgroundColor = [UIColor redColor];
            return cell;
            
            
        } else if ([model.type isEqualToString:@"largeLandScape"] || [model.type isEqualToString:@"largeLandScapeNodesc"]){
            HGLargeNormalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HGLargeNormalCollectionViewCell" forIndexPath:indexPath];
            cell.arr = model.arrModel;
            cell.backgroundColor = [UIColor whiteColor];
            return cell;
            
        } else if ([model.type isEqualToString:@"normalAvatorText"]){
           
            HGNormalAvatorCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HGNormalAvatorCollectionViewCell" forIndexPath:indexPath];
            cell.arr = model.arrModel;
            cell.delegate = self;
            
            return cell;
            
        } else if ([model.type isEqualToString:@"moviePortrait"]){
            HGMoviePorCollectionViewCell *Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HGMoviePorCollectionViewCell" forIndexPath:indexPath];
            Cell.arr = model.arrModel;
            Cell.delegate = self;
            
            return Cell;
 
        }else if ([model.type isEqualToString:@"textMoreLink"]){
            HGTextCollectionViewCell *Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HGTextCollectionViewCell" forIndexPath:indexPath];
            
            return Cell;
            
        } else if ([model.type isEqualToString:@"unknowModType1"]){
            HGNormalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HGNormalCollectionViewCell" forIndexPath:indexPath];
            cell.arr = model.arrModel;
            cell.backgroundColor = [UIColor redColor];
            return cell;
            

        } else if ([model.type isEqualToString:@"unknowModType2"]){
            HGNormalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HGNormalCollectionViewCell" forIndexPath:indexPath];
            cell.arr = model.arrModel;
            cell.backgroundColor = [UIColor redColor];
            return cell;
        }else{
            UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
            
            cell.backgroundColor = [UIColor yellowColor];
            return cell;
        }
        
    }
    
    
    
    
    
    
    
    
    
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(SREEN_WIDTH, 200);
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    TVHomeModel *model = _dataArr[indexPath.row];
    if ([model.type isEqual:[NSNull null]]) {
        {
            if (indexPath.row == 0) {
                return CGSizeMake(SREEN_WIDTH - 20, 0);
                
            } else {
                TVHomeModel *model = _dataArr[indexPath.row - 1];
                if ([model.type isEqualToString:@"title"]) {
                    return CGSizeMake(SREEN_WIDTH-1, 30);
                    
                } else if ([model.type isEqualToString:@"normalLandScape"] || [model.type isEqualToString:@"normalLandScapeNodesc"]){
                    return CGSizeMake(SREEN_WIDTH-1, 160);
                } else if ([model.type isEqualToString:@"normalAvatorText"]){
                    return CGSizeMake(SREEN_WIDTH-1, 80);
                }else if ([model.type isEqualToString:@"moviePortrait"]){
                    return CGSizeMake(SREEN_WIDTH-1, 260);
                } else if ([model.type isEqualToString:@"textMoreLink"]){
                    return CGSizeMake(SREEN_WIDTH - 1, 50);
                }else {
                    return CGSizeMake(SREEN_WIDTH, 220);
                }
                
                
                
                
            }
        }
        
    }else{
        {
            if (indexPath.row == 0) {
                return CGSizeMake(SREEN_WIDTH - 20, 0);
                
            } else {
                TVHomeModel *model = _dataArr[indexPath.row - 1];
                if ([model.type isEqualToString:@"title"]) {
                    return CGSizeMake(SREEN_WIDTH-1, 30);
                    
                } else if ([model.type isEqualToString:@"normalLandScape"] || [model.type isEqualToString:@"normalLandScapeNodesc"]){
                    return CGSizeMake(SREEN_WIDTH-1, 160);
                } else if ([model.type isEqualToString:@"normalAvatorText"]){
                    return CGSizeMake(SREEN_WIDTH-1, 80);
                }else if ([model.type isEqualToString:@"moviePortrait"]){
                    return CGSizeMake(SREEN_WIDTH-1, 260);
                } else if ([model.type isEqualToString:@"textMoreLink"]){
                    return CGSizeMake(SREEN_WIDTH - 1, 50);
                }else {
                    return CGSizeMake(SREEN_WIDTH, 220);
                }
                
                
                
                
            }
        }
    }
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    TVHomeModel *model = _dataArr[indexPath.row-1];
    ///判段type的类型来挑传页面
    if ([model.type isEqualToString:@"largeLandScape"] || [model.type isEqualToString:@"largeLandScapeNodesc"]) {
        TVModel *model1 = [model.arrModel firstObject];
        
        SecViewController *sec = [[SecViewController alloc] init];
        sec.url = model1.playUrl;
        
        [self presentViewController:sec animated:YES completion:^{
            
        }];
        
        
    }
    
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        if (indexPath.section == 0) {
            MovieSecZoneCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"MovieSecZoneCollectionReusableView" forIndexPath:indexPath];
            
            
            return header;
        }else{
            MovieSecZoneCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"MovieSecZoneCollectionReusableView" forIndexPath:indexPath];
            header.image1.image = [UIImage imageNamed:@"iconfont-logo.png"];
         
            
            header.backgroundColor = [UIColor whiteColor];
            return header;
            
        }
    }
    
    return nil;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

///数据处理
- (void)handleData{
    
    [APPTools GETWithURL:_url par:nil success:^(id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        //取出首页总数据
        NSArray *data = dic[@"data"];
        //取出轮播图数据
        for (NSDictionary *dic1 in data) {
            if ([dic1[@"type"] isEqualToString:@"banner"]) {
                NSArray *arr = dic1[@"templateData"];
                self.bannerArr = [TVModel baseModelHandleWithArr:arr];
            }else if ([dic1[@"type"] isEqualToString:@"textRollLink"]){
                NSArray *arr = dic1[@"templateData"];
                self.textRollLinkArr = [TVModel baseModelHandleWithArr:arr];
            }else if ([dic1[@"type"] isEqualToString:@"splitLabel"]){
                
               
            }else if ([dic1[@"type"] isEqualToString:@"largePortrait"]){
                
            }else if ([dic1[@"type"] isEqualToString:@"roundAvatorText"]){
               
            }else{
                TVHomeModel *model = [[TVHomeModel alloc] init];
                [model setValuesForKeysWithDictionary:dic1];
                
                [_dataArr addObject:model];
                
                NSLog(@"%@", model.type);
            }
        }
        
        
        
        
        [self createLunBo];
        [_collection reloadData];
    } filed:^(NSError *error) {
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
